package xcmg.device.service.report;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.report.InboundSummaryDO;
import xcmg.device.dao.mapper.report.InboundSummaryMapper;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.report.InboundSummaryConditionVO;
import xcmg.device.service.vo.report.InboundSummaryVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class InboundSummaryService {

    @Autowired
    private InboundSummaryMapper inboundSummaryMapper;

    @Autowired
    private HttpServletResponse response;

    public PageCommonVO pageList(SearchCommonVO<InboundSummaryConditionVO> conditionVO, String orgId) {
        conditionVO.getFilters().setOrgId(orgId);
        PageHelperPlus.startPage(conditionVO.getPageNum(), conditionVO.getPageSize());
        List<InboundSummaryDO> InboundSummaryDOs = inboundSummaryMapper.list(conditionVO.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(InboundSummaryDOs));
        List<InboundSummaryVO> inboundSummaryVOs = new ArrayList<InboundSummaryVO>();
        for (InboundSummaryDO inboundSummaryDO : InboundSummaryDOs) {
            if (inboundSummaryDO.getBillAmountWithTax() != null) {
                inboundSummaryDO.setBillAmountWithoutTax(inboundSummaryDO.getBillAmountWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            if (inboundSummaryDO.getCostPriceWithTax() != null) {
                inboundSummaryDO.setCostPriceWithoutTax(inboundSummaryDO.getCostPriceWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            InboundSummaryVO inboundSummaryVO = new InboundSummaryVO();
            BeanUtils.copyProperties(inboundSummaryDO, inboundSummaryVO);
            inboundSummaryVOs.add(inboundSummaryVO);
        }
        pageCommonVO.setPageInfoList(inboundSummaryVOs);
        return pageCommonVO;
    }

    public void export(InboundSummaryConditionVO condition, String orgId) throws Exception {
        condition.setOrgId(orgId);
        List<InboundSummaryDO> doList = inboundSummaryMapper.list(condition);
        String title = "备件入库汇总表";
        String[] rowsName = new String[]{"序号", "NC客户编码/供应商编码", "PMS客户代码/供应商编码", "客户名称/供应商名称", "客户/供应商分类", "出库单号", "预留单号", "采购单号", "退货申请单号",
                "其他入库类型", "仓库", "库区", "创建人", "创建时间", "入库时间", "零件编号", "零件名称", "计量单位", "数量", "含税成本价", "含税成本合计金额", "计划价", "成本价（不含税）", "备注"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < doList.size(); i++) {
            InboundSummaryDO e = doList.get(i);
            if (e.getBillAmountWithTax() != null) {
                e.setBillAmountWithoutTax(e.getBillAmountWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            if (e.getCostPriceWithTax() != null) {
                e.setCostPriceWithoutTax(e.getCostPriceWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = e.getCustomNcCode();
            objs[2] = e.getCustomPmsCode();
            objs[3] = e.getCustomName();
            objs[4] = e.getCustomType();
            objs[5] = e.getInBillNo();
            objs[6] = e.getReserveBillNo();
            objs[7] = e.getPurchaseBillNo();
            objs[8] = e.getReturnBillNo();
            objs[9] = e.getInType();
            objs[10] = e.getWarehouseCode();
            objs[11] = e.getAreaCode();
            objs[12] = e.getCreateName();
            objs[13] = e.getCreateTime()==null?"":sdf.format(e.getCreateTime());
            objs[14] = e.getInTime()==null?"":sdf.format(e.getInTime());
            objs[15] = e.getMaterialsNo();
            objs[16] = e.getMaterialsDesc();
            objs[17] = e.getUnit();
            objs[18] = e.getQuantity();
            objs[19] = e.getCostPriceWithTax();
            objs[20] = e.getBillAmountWithTax();
            objs[21] = e.getPlanPrice();
            objs[22] = e.getCostPriceWithoutTax();
            objs[23] = e.getRemark();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
